<template>
  <div>
    <el-drawer
      v-model="drawers"
      direction="rtl"
      size="380px"
      :show-close="false"
      @closed="handleClose"
      class="drawer-container"
      :close-on-click-modal="true"
      :with-header="false"
    >
      <div class="box">
        <div class="drawertop f-j-sb-a-c f-a-c">
          <div
            class="item click f-d-c f-j-a-c"
            v-for="(item, index) in top"
            :key="index"
            @click="topClick(index)"
          >
            <el-badge
              :value="item.nums"
              :max="99"
              class="item"
              :hidden="item.nums > 0 ? false : true"
            >
              <div class="itemspan f-a-c">
                <div class="itemline f-j-a-c f-d-c">
                  <span :class="topCurrent == index ? 'color' : ''">{{
                    item.title
                  }}</span>
                  <div class="line" v-if="topCurrent == index"></div>
                </div>
              </div>
            </el-badge>
          </div>
        </div>
        <!-- 系统 -->
        <div class="actice f-d-c" v-if="topCurrent == 3">
          <div
            class="activeitem f-d-c"
            v-for="(item, index) in activeList"
            :key="index"
            @click="activeListClick(item)"
          >
            <span>{{ item.eventDetail }}</span>
            <span class="time">{{ item.startTime }}</span>
            <img class="" :src="item.thumbnail" alt="" />
          </div>
        </div>
        <!-- 通知 -->
        <div
          class="tongzhi"
          v-if="topCurrent == 0 || topCurrent == 1 || topCurrent == 2"
          ref="observer"
          @scroll="handleScroll"
        >
          <div
            class="tongzhiitem"
            v-for="(item, index) in tonzhilist"
            :key="index"
          >
            <div class="tongzhitop f-d-c">
              <div class="tonzhis f-a-c">
                <img
                  class="svg"
                  v-if="item.icon_type == 7005"
                  src="../assets/image/icon-tz-yue.png"
                  alt=""
                />
                <img
                  class="svg"
                  v-else-if="item.icon_type == 5002"
                  src="../assets/image/icon-xt.png"
                  alt=""
                />
                <img
                  class="svg"
                  v-else
                  src="../assets/image/tx@2x.png"
                  alt=""
                />
                <div class="topitem f-a-c">
                  <span class="toptitle"
                    >{{ item.title }}
                    <span
                      class="topcontent"
                      style="color: #656f79"
                      v-html="item.contents"
                    ></span>
                  </span>
                </div>
              </div>
              <div class="time">
                {{ item.created_at }}
              </div>
            </div>
          </div>
          <noData v-if="!tonzhilist.length"></noData>
        </div>
        <!-- 点赞收藏 -->
        <!-- <div
          class="tongzhi"
          v-if="topCurrent == 1"
          ref="observer"
          @scroll="handleScroll"
        >
          <div
            class="tongzhiitem"
            v-for="(item, index) in tonzhilist"
            :key="index"
          >
            <div class="tongzhitop f-d-c">
              <div class="tonzhis f-a-c">
                <img
                  class="svg"
                  v-if="item.icon_type == 7005"
                  src="../assets/image/icon-tz-yue.png"
                  alt=""
                />
                <img
                  class="svg"
                  v-else-if="item.icon_type == 5002"
                  src="../assets/image/icon-xt.png"
                  alt=""
                />
                <img
                  class="svg"
                  v-else
                  src="../assets/image/tx@2x.png"
                  alt=""
                />
                <div class="topitem f-a-c">
                  <span class="toptitle"
                    >{{ item.title }}
                    <span
                      class="topcontent"
                      style="color: #656f79"
                      v-html="item.contents"
                    ></span>
                  </span>
                </div>
              </div>
              <div class="time">
                {{ item.created_at }}
              </div>
            </div>
          </div>
        </div> -->
      </div>
    </el-drawer>
  </div>
</template>

<script>
import noData from "@/components/noData.vue";
import { getevent, eventRead } from "@/axios/api.js";
import { useRouter } from "vue-router";
import {
  computed,
  defineComponent,
  onMounted,
  reactive,
  toRefs,
  getCurrentInstance,
  onUnmounted,
  ref,
} from "vue";
import { getDetailsMessages, readMessages, info } from "@/axios/community.js";
import { useStore } from "vuex";
export default defineComponent({
  components: {
    noData,
  },
  props: {
    drawers: {
      // 判断打开
      type: Boolean,
      default: false,
    },
  },

  setup(props, { attrs, slots, emit }) {
    const observer = ref(null);

    const router = useRouter();
    const { proxy } = getCurrentInstance();
    let store = useStore();
    let data = reactive({
      page: 0,
      isOver: false, //是否没有更多

      tonzhilist: [],
      activeList: [],
      topCurrent: 0,
      top: [
        {
          title: "通知",
          nums: 0,
        },
        {
          title: "点赞收藏",
          nums: 0,
        },
        {
          title: "评论",
          nums: 0,
        },
      ],
    });
    // java读取消息
    let geteventRead = () => {
      eventRead().then((res) => {});
    };
   
    // 读取消息
    let postreadMessages = (type) => {
      let id = "?type=" + type;
      readMessages({ id }).then((res) => {
        // getDetailsMessagesNow();
        getinfo();
      });
    };
    // 关闭弹窗
    let handleClose = () => {
      emit("closeinfo");
    };
    // 获取活动列表
    let geteventlist = () => {
      getevent({ page: 1, limit: 999 }).then((res) => {
        data.activeList = res.data.records;
        res.data.records.forEach((item, index) => {
          if (item.is_read == 0) {
            data.top[3].nums += 1;
          }
        });
      });
    };
    // 去除a标签
    let extractContent = (str) => {
      return str.replace(/<a\b[^>]*>(.*?)<\/a>/gi, function (match, p1) {
        return p1.replace(/\[|\]/g, "");
      });
    };
    // 未读消息
    let getinfo = () => {
      info().then((res) => {
        data.top[0].nums = res.data.list[0].noticeSystemCount;
        data.top[1].nums = res.data.list[1].noticeLikeCollectCount;
        data.top[2].nums = res.data.list[2].noticeCommentCount;
        let unreadNum = data.top[0].nums + data.top[1].nums + data.top[2].nums;
        store.commit("setunreadNum", unreadNum);
      });
    };
    // 消息
    let getDetailsMessagesNow = (type, index) => {
      console.log("消息");
      if (!data.isOver) {
        data.page += 1;
      } else {
        return;
      }
      if (type == true) {
        setTimeout(() => {
          getDetailsMessagesNow(false, index);
        }, 150);
      }
      let id = "?type=" + index + "&page=" + data.page;
      getDetailsMessages({ id }).then((res) => {
        let datalist = res.data.data;
        data.isOver = datalist.length < 10;
        if (datalist) {
          if (data.page == 1) data.tonzhilist = [];
          data.tonzhilist = data.tonzhilist.concat(datalist);
          data.tonzhilist = [...data.tonzhilist];
          data.tonzhilist.forEach((item, index) => {
            item.contents = extractContent(item.content);
          });
        } else {
          data.isOver = true;
        }
      });
    };
    onMounted(() => {
      getDetailsMessagesNow(true, 0); //通知
      getinfo();
      postreadMessages(0);
    });
    onUnmounted(() => {});
    let topClick = (index) => {
      data.topCurrent = index;
      data.tonzhilist = [];
      // if (data.topCurrent == 1) {
      //   postreadMessages(0);
      // }
      // console.log(data.topCurrent);
      data.page = 0;
      data.isOver = false;
      if (data.topCurrent != 3) {
        getDetailsMessagesNow(true, data.topCurrent);
        postreadMessages(data.topCurrent == 1 ? 1 : data.topCurrent);
      }
    };
    let activeListClick = () => {};
    let handleScroll = () => {
      let observerElement = observer.value;
      if (
        observerElement.scrollTop + observerElement.clientHeight >=
        observerElement.scrollHeight - 5
      ) {
        // 滚动到底部时加载更多数据
        getDetailsMessagesNow(false, data.topCurrent);
      }
    };
    return {
      getinfo,
      activeListClick,
      geteventRead,
      postreadMessages,
      observer,
      handleScroll,
      extractContent,
      geteventlist,
      getDetailsMessagesNow,
      topClick,
      handleClose,
      ...toRefs(data),
    };
  },
});
</script>
<style lang="scss" scoped>
.tongzhi {
  height: 1160px;
  overflow: auto;
  .svg {
    display: block;
    width: 40px;
    height: 40px;
  }
  .tongzhiitem {
    padding: 20px 0;

    .topitem {
      margin-left: 10px;

      .toptitle {
        font-size: 14px;
        font-family: Microsoft YaHei-Bold, Microsoft YaHei;
        font-weight: 700;
        color: #000000;
      }
      .topcontent {
        font-size: 14px;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
        font-weight: 400;
        color: #656f79;
      }
    }
  }
  .time {
    margin-top: 10px;

    margin-left: 50px;
    font-size: 12px;
    font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    font-weight: 400;
    color: #656f79;
  }
}

.actice {
  .activeitem {
    > span {
      margin-top: 10px;
      &:first-child {
        font-size: 14px;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
        font-weight: 400;
        color: #000000;
      }
    }
    .time {
      font-size: 12px;
      font-family: Microsoft YaHei-Regular, Microsoft YaHei;
      font-weight: 400;
      color: #656f79;
    }
    > img {
      margin-top: 10px;
      border-radius: 10px;
      object-fit: cover;
      width: 340px;
      height: 180px;
    }
  }
}

.box {
  // padding: 20px 20px;
  .drawertop {
    border-bottom: 1px solid #f5f6f7;
    .item {
      position: relative;
      // width: 24%;
      .redpop {
        margin-left: 2px;
        width: 12px;
        height: 12px;
        background: #ff1111;
        font-size: 6px;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
        font-weight: 400;
        color: #ffffff;
        border-radius: 50%;
      }
      .color {
        color: #44c6e5 !important;
      }
      .itemspan {
        align-items: flex-start;
        .itemline {
          > span {
            margin-bottom: 6px;
            font-size: 14px;
            font-family: Microsoft YaHei-Bold, Microsoft YaHei;
            // font-weight: 700;
            color: #000000;
          }
        }
      }

      &:first-child {
        margin-left: 0;
      }
    }
    .line {
      width: 30px;
      height: 3px;
      background: #44c6e5;
      border-radius: 4px 4px 0px 0px;
      opacity: 1;
    }
  }
}
::v-deep .el-drawer {
  border-radius: 10px;
}
::v-deep .el-overlay {
  background-color: transparent !important;
  // height:100%;
  // top: 80px;
  // right: 10px;
  // overflow: none !important;
  //   box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, 0.1);
}
::v-deep .drawer-container {
  position: absolute;
  height: 93%;
  top: 80px;
}
.tongzhi::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

/* 滚动条滑块样式 */
.tongzhi::-webkit-scrollbar-thumb {
  background-color: #c1c3c7;
  border-radius: 2px;
}

/* 滚动条轨道样式 */
.tongzhi::-webkit-scrollbar-track {
  background-color: #f7f9fa;
  height: 80% !important;
}

/* 鼠标悬停时滚动条轨道样式 */
.tongzhi::-webkit-scrollbar-track:hover {
  background-color: #f7f9fa;
}
</style>
